package com.heima.wemedia.controller.v1;


import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmNewsMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    @Autowired
    private WmMaterialService wmMaterialService;
    @Autowired
    private WmNewsMaterialService wmNewsMaterialService;

    @PostMapping("/upload_picture")
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        return wmMaterialService.uploadPicture(multipartFile);
    }

    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmMaterialDto dto) {
        return wmMaterialService.queryImages(dto);
    }

    @GetMapping("del_picture/{id}")
    public ResponseResult delPic(@PathVariable("id") Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        List<WmNewsMaterial> list = wmNewsMaterialService.list(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId, id));
        if (CollectionUtils.isNotEmpty(list)) {
            return ResponseResult.errorResult(501,"图片已被文章引用，不能删除");
        }
        boolean b = wmMaterialService.removeById(id);
        if (b) {
            return ResponseResult.success();
        } else {
            return ResponseResult.errorResult(501, "文件删除失败");
        }
    }

    @GetMapping("collect/{id}")
    public ResponseResult collect(@PathVariable("id") Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmMaterial entity = new WmMaterial();
        entity.setId(id);
        entity.setIsCollection((short) 1);
        boolean b = wmMaterialService.updateById(entity);
        if (b) {
            return ResponseResult.success();
        } else {
            return ResponseResult.errorResult(501, "收藏失败");
        }
    }

    @GetMapping("cancel_collect/{id}")
    public ResponseResult cancel_collect(@PathVariable("id") Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmMaterial entity = new WmMaterial();
        entity.setId(id);
        entity.setIsCollection((short) 0);
        boolean b = wmMaterialService.updateById(entity);
        if (b) {
            return ResponseResult.success();
        } else {
            return ResponseResult.errorResult(501, "取消收藏失败");
        }
    }
    public static void main(String[] args) {
        File file = new File("D:\\zzzzzzzzzzzzzzzzzzz");
        String[] list = file.list();
        for (String s : list != null ? list : new String[0]) {
            System.out.println(s.replace("GT4-税务总局电子发票服务平台-杨珩-","")
            .replace("-周报.docx",""));
        }
    }

}
